package com.eboss2.sbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.model.PointActivateShop;
import com.eboss2.sbs.model.RangeEntity;

import java.util.List;


public class PointActivateShopListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<PointActivateShop> datas;
	private LayoutInflater mInflater;
	
	public PointActivateShopListAdapter(){}
	public PointActivateShopListAdapter(Context context, List<PointActivateShop> datas){
		this.context = context;
		this.datas = datas;
		this.mInflater = LayoutInflater.from(this.context);
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public RangeEntity getChild(int arg0, int arg1) {
		if (datas.get(arg0).getShopRanges() != null)
			return datas.get(arg0).getShopRanges().get(arg1);
		else{return null;}
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		if (datas.get(groupPosition).getShopRanges() != null){return childPosition;}
		else{return 0;}
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		if (datas.get(groupPosition).getShopRanges() != null) {
			ChildViewHolder childViewHolder = null;
			if(convertView==null){
				childViewHolder=new ChildViewHolder();
				convertView = mInflater.inflate(R.layout.expandable_shop_point_item, null);
				childViewHolder.range = (TextView) convertView.findViewById(R.id.range);
				childViewHolder.value = (TextView) convertView.findViewById(R.id.value);
				convertView.setTag(childViewHolder);
			}
			else{childViewHolder=(ChildViewHolder) convertView.getTag();}
			childViewHolder.range.setText(datas.get(groupPosition).getShopRanges().get(childPosition).getRange());
			childViewHolder.value.setText(datas.get(groupPosition).getShopRanges().get(childPosition).getValue());
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (datas.get(groupPosition).getShopRanges() != null){
			return datas.get(groupPosition).getShopRanges().size();
		}
		return 0;
	}

	@Override
	public PointActivateShop getGroup(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public int getGroupCount() {
		if(datas!=null&&datas.size()>0){return datas.size();}
		else return 0;
	}

	@Override
	public long getGroupId(int arg0) {
		return arg0;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		MenuViewHolder menuviewHolder = null;
		if(convertView==null){
			menuviewHolder=new MenuViewHolder();
			convertView = mInflater.inflate(R.layout.expandable_shop_point_menu, null);
			menuviewHolder.name = (TextView) convertView.findViewById(R.id.name);
			menuviewHolder.info = (TextView) convertView.findViewById(R.id.info);
			convertView.setTag(menuviewHolder);
		}
		else{menuviewHolder=(MenuViewHolder) convertView.getTag();}
		menuviewHolder.name.setText(datas.get(groupPosition).getName());
		menuviewHolder.info.setText("增加额度："+datas.get(groupPosition).getAddrate());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
	
	class MenuViewHolder {
		private TextView name;
		private TextView info;
	}
	
	class ChildViewHolder {
		private TextView range;
		private TextView value;
	}

}
